package com.alibaba.citrus.cr.forecast.facade.service;


import com.alibaba.citrus.cr.common.exception.FunctionException;
import com.alibaba.citrus.cr.forecast.common.MoneyUtil;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.crforecastsales.activityimport.dto.*;
import com.epoch.app.crforecastsales.activityimport.service.ActivityImportService;
import com.epoch.app.crforecastsales.domian.activityclientrelation.dto.CreateActivityClientRelationResponse;
import com.epoch.app.crforecastsales.domian.activityclientrelation.service.ActivityClientRelationService;
import com.epoch.app.crforecastsales.domian.activitytermgiftrelation.dto.CreateActivityTermGiftRelationResponse;
import com.epoch.app.crforecastsales.domian.activitytermgiftrelation.service.ActivityTermGiftRelationService;
import com.epoch.app.crforecastsales.domian.activitytermitemrelation.dto.CreateActivityTermItemRelationResponse;
import com.epoch.app.crforecastsales.domian.activitytermitemrelation.service.ActivityTermItemRelationService;
import com.epoch.app.crforecastsales.domian.activitytitleext.dto.CreateActivityTitleExtResponse;
import com.epoch.app.crforecastsales.domian.activitytitleext.service.ActivityTitleExtService;
import com.epoch.app.crforecastsales.domian.operatejournalflow.dto.CreateOperateJournalFlowResponse;
import com.epoch.app.crforecastsales.domian.operatejournalflow.service.OperateJournalFlowService;
import com.epoch.app.crforecastsales.domian.segmentedactivityterm.dto.CreateSegmentedActivityTermResponse;
import com.epoch.app.crforecastsales.domian.segmentedactivityterm.service.SegmentedActivityTermService;
import com.epoch.app.crforecastsales.model.dto.*;
import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Service
@Primary
public class ActivityImportServiceImpl implements ActivityImportService {
    @Resource
    private ActivityTitleExtService activityTitleExtService;

    @Resource
    private ActivityTermGiftRelationService activityTermGiftRelationService;

    @Resource
    private ActivityClientRelationService activityClientRelationService;

    @Resource
    private SegmentedActivityTermService segmentedActivityTermService;

    @Resource
    private ActivityTermItemRelationService activityTermItemRelationService;

    @Resource
    private OperateJournalFlowService operateJournalFlowService;


    /**
     * importGroupControlActivity
     */
    @Override
    public Result<List<ActivityTitleExtCreateRequestImport>> importGroupControlActivity(ImportGroupControlActivityRequest request) {
        if (Objects.isNull(request) || CollectionUtils.isEmpty(request.getInputRequest())) {
            return Result.fail("", "导入内容不能为空");
        }
        request.getInputRequest().forEach(o -> {
                    ActivityTitleExtCreateRequest activityTitleExtCreateRequest = new ActivityTitleExtCreateRequest();
                    BeanUtils.copyProperties(o, activityTitleExtCreateRequest);
                    activityTitleExtCreateRequest.setStatus(1);
                    CreateActivityTitleExtResponse activityTitleExt = null;
                    try {
                        activityTitleExt = activityTitleExtService.createActivityTitleExt(activityTitleExtCreateRequest);
                    } catch (Exception e) {
                        if (StringUtils.isNotBlank(e.getMessage()) && e.getMessage().contains("Duplicate entry")) {
                            o.setErrorMessage("创建失败！活动不满足唯一性!");
                            return;
                        }
                        o.setErrorMessage(e.getMessage());
                        e.printStackTrace();
                        return;

                    }
                    if (Objects.isNull(activityTitleExt)) {
                        o.setErrorMessage("创建失败！数据库操作返回为空！");
                        return;
                    }
                    if (StringUtils.isBlank(activityTitleExt.getLastInsertId())) {
                        o.setErrorMessage(activityTitleExt.getErrorMessage());
                    }
                }
        );
        return Result.success(request.getInputRequest());
    }

    /**
     * importGiftRelationActivity
     */
    @Override
    public Result<List<ActivityTermGiftRelationCreateRequestImport>> importGiftRelationActivity(ImportGiftRelationActivityRequest request) {
        if (Objects.isNull(request) || CollectionUtils.isEmpty(request.getInputRequest())) {
            return Result.fail("", "导入内容不能为空");
        }
        request.getInputRequest().forEach(o -> {
                    ActivityTermGiftRelationCreateRequest createRequest = new ActivityTermGiftRelationCreateRequest();
                    BeanUtils.copyProperties(o, createRequest);
                    createRequest.setStatus(1);
                    CreateActivityTermGiftRelationResponse activityTermGiftRelation = null;
                    try {
                        activityTermGiftRelation = activityTermGiftRelationService.createActivityTermGiftRelation(createRequest);
                    } catch (Exception e) {
                        if (StringUtils.isNotBlank(e.getMessage()) && e.getMessage().contains("Duplicate entry")) {
                            o.setErrorMessage("创建失败！活动不满足唯一性!");
                            return;
                        }
                        o.setErrorMessage(e.getMessage());
                        e.printStackTrace();
                        return;
                    }
                    if (Objects.isNull(activityTermGiftRelation)) {
                        o.setErrorMessage("创建失败！数据库操作返回为空！");
                        return;
                    }
                    if (StringUtils.isBlank(activityTermGiftRelation.getLastInsertId())) {
                        o.setErrorMessage(activityTermGiftRelation.getErrorMessage());
                    }
                }
        );
        return Result.success(request.getInputRequest());
    }

    /**
     * importCustomerRelationActivity
     */
    @Override
    public Result<List<ActivityClientRelationCreateRequestImport>> importCustomerRelationActivity(ImportCustomerRelationActivityRequest request) {
        if (Objects.isNull(request) || CollectionUtils.isEmpty(request.getInputRequest())) {
            return Result.fail("", "导入内容不能为空");
        }
        request.getInputRequest().forEach(o -> {
                    ActivityClientRelationCreateRequest createRequest = new ActivityClientRelationCreateRequest();
                    BeanUtils.copyProperties(o, createRequest);
                    CreateActivityClientRelationResponse activityClientRelation = null;
                    try {
                        activityClientRelation = activityClientRelationService.createActivityClientRelation(createRequest);
                    } catch (Exception e) {
                        if (StringUtils.isNotBlank(e.getMessage()) && e.getMessage().contains("Duplicate entry")) {
                            o.setErrorMessage("创建失败！活动不满足唯一性!");
                            return;
                        }
                        o.setErrorMessage(e.getMessage());
                        e.printStackTrace();
                        return;
                    }
                    if (Objects.isNull(activityClientRelation)) {
                        o.setErrorMessage("创建失败！数据库操作返回为空！");
                        return;
                    }
                    if (StringUtils.isBlank(activityClientRelation.getLastInsertId())) {
                        o.setErrorMessage(activityClientRelation.getErrorMessage());
                    }
                }
        );
        return Result.success(request.getInputRequest());
    }

    /**
     * importSegmentedActivity
     */
    @Override
    public Result<List<SegmentedActivityTermCreateRequestImport>> importSegmentedActivity(ImportSegmentedActivityRequest request) {
        if (Objects.isNull(request) || CollectionUtils.isEmpty(request.getInputRequest())) {
            return Result.fail("", "导入内容不能为空");
        }
        request.getInputRequest().forEach(o -> {
                    SegmentedActivityTermCreateRequest createRequest = new SegmentedActivityTermCreateRequest();
                    BeanUtils.copyProperties(o, createRequest);
                    if (StringUtils.isNotBlank(createRequest.getSpecialOfferPrice())) {
                        createRequest.setSpecialOfferPrice(
                                MoneyUtil.formatRoundLong(createRequest.getSpecialOfferPrice()).toString());
                    }

                    CreateSegmentedActivityTermResponse segmentedActivityTerm = null;
                    try {
                        segmentedActivityTerm = segmentedActivityTermService.createSegmentedActivityTerm(createRequest);
                    } catch (Exception e) {
                        if (StringUtils.isNotBlank(e.getMessage()) && e.getMessage().contains("Duplicate entry")) {
                            o.setErrorMessage("创建失败！活动不满足唯一性!");
                            return;
                        }
                        o.setErrorMessage(e.getMessage());
                        e.printStackTrace();
                        return;
                    }
                    if (Objects.isNull(segmentedActivityTerm)) {
                        o.setErrorMessage("创建失败！数据库操作返回为空!");
                        return;
                    }
                    if (StringUtils.isBlank(segmentedActivityTerm.getLastInsertId())) {
                        o.setErrorMessage(segmentedActivityTerm.getErrorMessage());
                    }
                }
        );
        return Result.success(request.getInputRequest());
    }

    /**
     * importItemRelationActivity
     */
    @Override
    public Result<List<ActivityTermItemRelationCreateRequestImport>> importItemRelationActivity(ImportItemRelationActivityRequest request) {
        if (Objects.isNull(request) || CollectionUtils.isEmpty(request.getInputRequest())) {
            throw new FunctionException("导入内容不能为空");
        }
        request.getInputRequest().forEach(o -> {
                    ActivityTermItemRelationCreateRequest createRequest = new ActivityTermItemRelationCreateRequest();
                    BeanUtils.copyProperties(o, createRequest);
                    CreateActivityTermItemRelationResponse activityTermItemRelation = null;
                    try {
                        activityTermItemRelation = activityTermItemRelationService.createActivityTermItemRelation(createRequest);
                    } catch (Exception e) {
                        if (StringUtils.isNotBlank(e.getMessage()) && e.getMessage().contains("Duplicate entry")) {
                            o.setErrorMessage("创建失败！活动不满足唯一性!");
                            return;
                        }
                        o.setErrorMessage(e.getMessage());
                        e.printStackTrace();
                        return;
                    }
                    if (Objects.isNull(activityTermItemRelation)) {
                        o.setErrorMessage("创建失败！数据库操作返回为空!");
                        return;
                    }
                    if (StringUtils.isBlank(activityTermItemRelation.getLastInsertId())) {
                        o.setErrorMessage(activityTermItemRelation.getErrorMessage());
                    }
                }
        );
        return Result.success(request.getInputRequest());
    }

    /**
     * importCombinationGiftActivity
     */
    @Override
    public Result<List<OperateJournalFlowCreateRequestImport>> importCombinationGiftActivity(ImportCombinationGiftActivityRequest request) {
        if (Objects.isNull(request) || CollectionUtils.isEmpty(request.getInputRequest())) {
            return Result.fail("", "导入内容不能为空");
        }
        request.getInputRequest().forEach(o -> {
                    OperateJournalFlowCreateRequest createRequest = new OperateJournalFlowCreateRequest();
                    BeanUtils.copyProperties(o, createRequest);
                    CreateOperateJournalFlowResponse operateJournalFlow = null;
                    try {
                        operateJournalFlow = operateJournalFlowService.createOperateJournalFlow(createRequest);
                    } catch (Exception e) {
                        if (StringUtils.isNotBlank(e.getMessage()) && e.getMessage().contains("Duplicate entry")) {
                            o.setErrorMessage("创建失败！活动不满足唯一性!");
                            return;
                        }
                        o.setErrorMessage(e.getMessage());
                        e.printStackTrace();
                        return;
                    }
                    if (Objects.isNull(operateJournalFlow)) {
                        o.setErrorMessage("创建失败！数据库操作返回为空!");
                        return;
                    }
                    if (StringUtils.isBlank(operateJournalFlow.getLastInsertId())) {
                        o.setErrorMessage(operateJournalFlow.getErrorMessage());
                    }
                }
        );
        return Result.success(request.getInputRequest());
    }
}
